﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace AutomatedTP
{
    /// <summary>
    /// Class for handle logging operation.
    /// </summary>
    public static class Logger
    {
        private static string path;
        public static int level { get; set; }
        public static bool verbose { get; set; }  
        
        /// <summary>
        /// Writes to log if level of parameter is greater equal to level set within this class.
        /// </summary>
        /// <param name="level_p">The level_p.</param>
        /// <param name="line">The line.</param>
        internal static void WriteToLog(int level_p, string line)
        {
            if (level_p >= level)
            {
                System.IO.StreamWriter file = new System.IO.StreamWriter(path, true);
                file.WriteLine(line);
                if (verbose)
                    Console.WriteLine(line);
                file.Close(); 
            }
        }

        /// <summary>
        /// Sets the path to file for logging.
        /// </summary>
        internal static void setPath()
        {
            string path_n = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "log.txt");

            //check path
            if (File.Exists(path_n))
            {
                //Console.WriteLine("Log file exist!");
            }
            else
            {
                //create file
                //Console.WriteLine("Log file does not exist and will be created!");
                File.Create(path_n).Close();
            }

            path = path_n;
        }

        /// <summary>
        /// Sets the level of logging.
        /// 1 - Fatal errors.
        /// 2 - Warnings.
        /// 3 - Basic information about progress.
        /// </summary>
        /// <param name="p">The p.</param>
        internal static void setLevel(int p)
        {
            if (p > 0)
            {
                level = p;
            }
            else {
                level = 1;
            }
            WriteToLog(2, "Level for logging set to: " + level);
        }
    }
}